package com.andy.ui.ext

import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.setActionButtonEnabled
import com.afollestad.materialdialogs.customview.customView
import com.andy.ui.R
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.haibin.calendarview.CalendarView.OnCalendarRangeSelectListener
import com.haibin.calendarview.CalendarView.OnMonthChangeListener

/**
 * @creator:LuoFan
 * @date:2022/6/23
 * @description:
 */

typealias DateRangeChooserCallback = (dialog: MaterialDialog, startTime: Long, endTime: Long) -> Unit

typealias DateChooserCallback = (dialog: MaterialDialog, selectTime: Long) -> Unit

fun MaterialDialog.dateRangeChooser(
    minTime: Long? = null,
    maxTime: Long? = null,
    currentTime: Long? = null,
    requireFutureDate: Boolean = false,
    dateCallback: DateRangeChooserCallback
): MaterialDialog {
    customView(R.layout.md_date_chooser)
    val calendarView = findViewById<CalendarView>(R.id.calendarView)
    calendarView.setSelectRangeMode()
    initCalendarView(calendarView, minTime, maxTime, currentTime, requireFutureDate)
    var startTime = 0L
    var endTime = 0L
    calendarView.setOnCalendarRangeSelectListener(object : OnCalendarRangeSelectListener {
        override fun onCalendarSelectOutOfRange(calendar: Calendar) {}
        override fun onSelectOutOfRange(calendar: Calendar, isOutOfMinRange: Boolean) {}
        override fun onCalendarRangeSelect(calendar: Calendar, isEnd: Boolean) {
            if (isEnd) {
                endTime = calendar.timeInMillis
                setActionButtonEnabled(WhichButton.POSITIVE, true)
            } else {
                startTime = calendar.timeInMillis
            }
        }
    })
    positiveButton {
        dateCallback.invoke(it, startTime, endTime)
    }
    return this
}

fun MaterialDialog.dateChooser(
    minTime: Long? = null,
    maxTime: Long? = null,
    currentTime: Long? = null,
    requireFutureDate: Boolean = false,
    dateCallback: DateChooserCallback
): MaterialDialog {
    customView(R.layout.md_date_chooser)
    val calendarView = findViewById<CalendarView>(R.id.calendarView)
    calendarView.setSelectSingleMode()
    initCalendarView(calendarView, minTime, maxTime, currentTime, requireFutureDate)
    calendarView.setOnCalendarSelectListener(object : CalendarView.OnCalendarSelectListener {
        override fun onCalendarOutOfRange(calendar: Calendar) {

        }

        override fun onCalendarSelect(calendar: Calendar, isClick: Boolean) {
        }
    })
    setActionButtonEnabled(WhichButton.POSITIVE, true)
    positiveButton {
        dateCallback.invoke(it, calendarView.selectedCalendar.timeInMillis)
    }
    return this
}

private fun MaterialDialog.initCalendarView(
    calendarView: CalendarView,
    minTime: Long? = null,
    maxTime: Long? = null,
    currentTime: Long? = null,
    requireFutureDate: Boolean = false,
): MaterialDialog {
    val monthText = findViewById<TextView>(R.id.tv_current_month)
    monthText.text = "${calendarView.curYear}年${calendarView.curMonth}月"
    if (minTime != null && maxTime != null) {
        val start = java.util.Calendar.getInstance()
        start.timeInMillis = minTime
        val startYear = start[java.util.Calendar.YEAR]
        val startMonth = start[java.util.Calendar.MONTH] + 1
        val startDay = start[java.util.Calendar.DAY_OF_MONTH]

        val end = java.util.Calendar.getInstance()
        end.timeInMillis = maxTime
        val endYear = end[java.util.Calendar.YEAR]
        val endMonth = end[java.util.Calendar.MONTH] + 1
        val endDay = end[java.util.Calendar.DAY_OF_MONTH]
        calendarView.setSelectCalendarRange(startYear, startMonth, startDay, endYear, endMonth, endDay)
    } else {
        if (requireFutureDate) {
            calendarView.setSelectCalendarRange(2000, 1, 1, 2100, 1, 1)
        } else {
            calendarView.setSelectCalendarRange(2000, 1, 1, calendarView.curYear, calendarView.curMonth, calendarView.curDay)
        }
    }
    calendarView.setOnMonthChangeListener(OnMonthChangeListener { year, month ->
        monthText.text = "${year}年${month}月"
    })
    setActionButtonEnabled(WhichButton.POSITIVE, false)
    negativeButton(text = "取消")
    calendarView.post {
        currentTime?.let {
            val select = java.util.Calendar.getInstance()
            select.timeInMillis = it
            val year = select[java.util.Calendar.YEAR]
            val month = select[java.util.Calendar.MONTH] + 1
            val day = select[java.util.Calendar.DAY_OF_MONTH]
            monthText.text = "${year}年${month}月"
            calendarView.scrollToCalendar(year, month, day)
        } ?: let {
            calendarView.scrollToCurrent()
        }
    }
    return this
}